package trinidad;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.Socket;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class NewNetGame extends JFrame implements ActionListener{
	private static final long serialVersionUID = 1L;
	static String windows = "com.sun.java.swing.plaf.windows.WindowsLookAndFeel";
    protected JPanel mainWindow;
    private NeTrinidadApp parent;
    private boolean kto = true;
    JTextField wpis;
    
    public NewNetGame(NeTrinidadApp parent, boolean kto, boolean new_game) {
    	super("Gra sieciowa");
    	this.parent = parent;
		this.setSize(200, 120);
		this.setResizable(false);

		try {
			UIManager.setLookAndFeel(windows);
		} catch (UnsupportedLookAndFeelException exc) {
		} catch (IllegalAccessException exc) {
		} catch (ClassNotFoundException exc) {
		} catch (InstantiationException exc) {
		}
		mainWindow = setMainWindow();
		if(new_game){
	    	JButton newGameButton = new JButton("Nowa gra");
	   		newGameButton.addActionListener(this);
	   		newGameButton.setActionCommand("newGameButton");
	   		mainWindow.add(newGameButton);
		} else {
	    	JButton newGameButton = new JButton("Dołącz się");
	   		newGameButton.addActionListener(this);
	   		newGameButton.setActionCommand("newGameButton");
	   		mainWindow.add(newGameButton);
		}
		getContentPane().add(mainWindow , BorderLayout.CENTER);
		this.setVisible(true);
	    this.setLocationRelativeTo(parent.winMain);
	    this.kto = kto;
    }

	private JPanel setMainWindow() {
		JPanel mainPanel = new JPanel();
   		mainPanel.setBorder(BorderFactory.createEmptyBorder(5,5,5,5));
   		mainPanel.setLayout(new BoxLayout(mainPanel,BoxLayout.PAGE_AXIS));

        JLabel ip = new JLabel("Komputer Zdalny(adres ip):");
        mainPanel.add(ip);
        wpis = new JTextField(20);
        mainPanel.add(wpis);

        JButton cancelButton = new JButton("Anuluj");
   		cancelButton.addActionListener(this);
   		cancelButton.setActionCommand("cancelButton");
   		mainPanel.add(cancelButton);
   		   		
		mainPanel.setOpaque(true);
		return mainPanel;
	}
	
	public void actionPerformed(ActionEvent e) {
        String cmd = e.getActionCommand();

        if (cmd.equals("cancelButton")) {
        	this.dispose();
        }
        else if(cmd.equals("newGameButton")) {       	
        	String opponent = "net";
        	
        	
        	boolean reverseturns = false;
        	boolean humanfirst = kto;
        	int maxdepth = 4;
        	int overrun = 100000;
        	String ip = wpis.getText();
        	ip = ip.equals("") ? "localhost" : ip ;
        	this.parent.setNetParams(ip,"0000",true);   
			this.parent.setParameters(maxdepth, overrun, opponent, reverseturns, humanfirst);
			this.parent.newGame();
			this.dispose();
        }
	}
    
}
